﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using BLL;
using BLL.Access;
using Newtonsoft.Json;
using Platform.Menu;
using Web.Model;

namespace Web.ashx
{
    /// <inheritdoc />
    /// <summary>
    ///     UserMenu 的摘要说明
    /// </summary>
    public class UserMenu : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            var code = context.Request.Cookies["code"].Value;
            var userId = Convert.ToInt32(context.Request["userid"]); // LoginHelper.UserID;
            var helper = new SpHelper();
            var dt = helper.QR_MENU(userId, code).Tables[0];
            var userMenu = new SqlHelper().GetUserMenu(userId,code);
            var userMenus = new string[0];
            if (userMenu != null)
                userMenus = userMenu.Split(',');

            var menuList = new List<MenuItem>();
            for (var i = 0; i < dt.Rows.Count; i++)
            {
                var menuItem = new MenuItem
                {
                    BelongID = dt.Rows[i]["BELONGID"] == null ? 0 : Convert.ToInt32(dt.Rows[i]["BELONGID"]),
                    DisplayIndex = dt.Rows[i]["DISPLAYINDEX"] == null ? 0 : Convert.ToInt32(dt.Rows[i]["DISPLAYINDEX"]),
                    Event = Convert.ToString(dt.Rows[i]["EVENT"]),
                    ImageLink = Convert.ToString(dt.Rows[i]["IMAGELINK"]),
                    MdiChildForm = dt.Rows[i]["MDICHILDFORM"] == null ? 0 : Convert.ToInt32(dt.Rows[i]["MDICHILDFORM"]),
                    MenuHeight = dt.Rows[i]["MENUHEIGHT"] == DBNull.Value
                        ? 0
                        : Convert.ToInt32(dt.Rows[i]["MENUHEIGHT"]),
                    MenuID = dt.Rows[i]["MENUID"] == null ? 0 : Convert.ToInt32(dt.Rows[i]["MENUID"]),
                    MenuName = Convert.ToString(dt.Rows[i]["MENUNAME"]),
                    MenuWidth = dt.Rows[i]["MENUWIDTH"] == DBNull.Value ? 0 : Convert.ToInt32(dt.Rows[i]["MENUWIDTH"]),
                    PermissionID = Convert.ToString(dt.Rows[i]["PERMISSIONID"])
                };
                var PICMODE = dt.Rows[i]["PICMODE"] == DBNull.Value ? 0 : Convert.ToInt32(dt.Rows[i]["PICMODE"]);
                switch (PICMODE)
                {
                    case 0:
                        menuItem.PicMode = MenuItemPicMode.None;
                        break;
                    case 1:
                        menuItem.PicMode = MenuItemPicMode.Small;
                        break;
                    case 2:
                        menuItem.PicMode = MenuItemPicMode.Large;
                        break;
                }
                menuItem.Remark = Convert.ToString(dt.Rows[i]["REMARK"]);
                menuItem.Status = dt.Rows[i]["STATUS"] == null ? 0 : Convert.ToInt32(dt.Rows[i]["STATUS"]);
                menuItem.WindowMode =
                    dt.Rows[i]["WINDOWSTATE"] == null ? 0 : Convert.ToInt32(dt.Rows[i]["WINDOWSTATE"]);
                menuItem.WindowState = dt.Rows[i]["WINDOWMODE"] == null ? 0 : Convert.ToInt32(dt.Rows[i]["WINDOWMODE"]);
                menuList.Add(menuItem);
            }
            var parentList = menuList.Where(m => m.BelongID == 0).OrderBy(m => m.DisplayIndex).ToList();
            var menus = new List<UserMenuItem>();
            foreach (var parentItem in parentList)
            {
                var userMenuItem = new UserMenuItem
                {
                    Name = parentItem.MenuName,
                    UserChildMenuItems = new List<UserChildMenuItem>()
                };
                var childMenus = menuList.Where(m => m.BelongID == parentItem.MenuID).OrderBy(m => m.DisplayIndex)
                    .ToList();
                foreach (var childMenu in childMenus)
                {
                    var secondChilds = menuList.Where(m => m.BelongID == childMenu.MenuID).OrderBy(m => m.DisplayIndex)
                        .ToList();
                    if (secondChilds.Count != 0)
                    {
                        foreach (var secondChild in secondChilds)
                        {
                            var userChildMenu = new UserChildMenuItem
                            {
                                MenuName = secondChild.MenuName,
                                MenuId = secondChild.MenuID
                            };
                            userChildMenu.Checked = userMenus.Contains(secondChild.MenuID.ToString());
                            userMenuItem.UserChildMenuItems.Add(userChildMenu);
                        }
                    }
                    else
                    {
                        var userChildMenu = new UserChildMenuItem
                        {
                            MenuName = childMenu.MenuName,
                            MenuId = childMenu.MenuID
                        };
                        userChildMenu.Checked = userMenus.Contains(childMenu.MenuID.ToString());
                        userMenuItem.UserChildMenuItems.Add(userChildMenu);
                    }
                }
                menus.Add(userMenuItem);
            }
            context.Response.ContentType = "application/json";
            context.Response.Write(JsonConvert.SerializeObject(menus));
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}